<?php

// $Id$


require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_page_w_fctrl_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/forms/vpage/vpage_form_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_template_dbc.class.php');


class VPage_Edit_WP extends TCMS_Admin_Page_W_FCtrl_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_vpage/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('category');
		$this->add_transit_var('return_to');
	}


	protected function create_form_ctrl() {
		$dbc = $this->get_dbc();
		$tvm = $this->get_tvm();

		$config = $this->get__config();

		$ret = new VPage_Form_Ctrl('vpage', $tvm, $dbc, 'obj_id', $config->get_htdocs_dir(), $config->get_hidden_dir().'inc/');

		return $ret;
	}


	protected function on_add_new() {
		$current_user = $this->get_user();
		$this->form_ctrl->set_permanent_external_param('user', $current_user->get_id());

		$this->form_ctrl->set_permanent_external_param('add_new', 1);
		$this->export('add_new', 1);

		$category_id = $this->get_transit_var('category', true);
		if ($category_id) {
			$this->form_ctrl->set_permanent_external_param('category_id', $category_id);
			$this->export('category_id', $category_id);
		}
	}


	protected function on_received_id($object_id) {
		$current_user = $this->get_user();
		$this->form_ctrl->set_permanent_external_param('user', $current_user->get_id());

		$this->export('vpage_id', $object_id);

		$category_id = $this->get_transit_var('category', true);
		if ($category_id) {
			$this->export('category_id', $category_id);
		}
	}


	protected function create_default_redir_view() {
		$object = $this->form_ctrl->get_object();
		$return_to = $this->get_transit_var('return_to');
		if ($return_to) {
			$redir_view = $this->create_redirect_view($return_to, array('manage' => $object->get_id()));
		} else {
			$dbc = $this->get_dbc();
			$vp_template = new TCMS_VPage_Template_DBC();
			if ($vp_template->load_by_id($dbc, $object->get_template())) {
				if ($vp_template->get_man_path()) {
					$redir_view = $this->create_redirect_view($vp_template->get_man_path(), array('manage' => $object->get_id()));
				} else {
					$redir_view = $this->create_redirect_view($this->get_redir_on_success(), array('manage' => $object->get_id()));
				}
			} else {
				throw new Tangra_Module_Exception('Cannot load vpage template with ID = '.$object->get_template());
			}
		}

		return $redir_view;
	}
}